package me.fanchaoo.web.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.fanchaoo.enums.EnumFileType;
import me.fanchaoo.exception.BusinessException;
import me.fanchaoo.util.FileTypeUtils;
import me.fanchaoo.util.UUIDUtils;
import me.fanchaoo.util.UserContext;
import me.fanchaoo.web.service.OssService;
import me.fanchaoo.web.util.oss.OssConfig;
import me.fanchaoo.web.util.oss.OssUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.net.www.content.image.gif;
import sun.net.www.content.image.jpeg;
import sun.net.www.content.image.png;

import java.io.IOException;

@Slf4j
@Service
public class OssServiceImpl implements OssService {

    @Autowired
    private OssConfig ossConfig;

    @Override
    public String uploadImageOrVideo(MultipartFile file) throws Exception {
        log.info("开始上传");
        // 校验文件类型
        String flieType = checkFileTypeWithContentType(file);

        String artistName = UserContext.getLoginUser().getArtistName();

        String fileDir = artistName + (flieType== "image"? "/image/" : flieType== "video" ?"/video/":"file");
        String filePath = fileDir + System.currentTimeMillis() + UUIDUtils.getUUID() + FileTypeUtils.getSuffix(file.getOriginalFilename());

        byte[] bytes = file.getBytes();

        long start = System.currentTimeMillis();
        OssUtils.uploadFile(bytes, filePath, file.getOriginalFilename());
        log.info("上传花费：" + (System.currentTimeMillis() - start));
        return ossConfig.getDomainUrl() + filePath;
    }

    private String checkFileTypeWithContentType(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        String type="";
//        boolean isImage = StringUtils.endsWithIgnoreCase(contentType, "png")
//                || StringUtils.endsWithIgnoreCase(contentType, "jpg")
//                || StringUtils.endsWithIgnoreCase(contentType, "jpeg")
//                || StringUtils.endsWithIgnoreCase(contentType, "gif");
//
//        boolean isVideo = StringUtils.endsWithIgnoreCase(contentType, "flv")
//                || StringUtils.endsWithIgnoreCase(contentType, "mp4")
//                || StringUtils.endsWithIgnoreCase(contentType, "png")
//                || StringUtils.endsWithIgnoreCase(contentType, "avi");
//        boolean isFile = StringUtils.endsWithIgnoreCase(contentType, "docx")
//                || StringUtils.endsWithIgnoreCase(contentType, "doc")
//                || StringUtils.endsWithIgnoreCase(contentType, "xls")
//                || StringUtils.endsWithIgnoreCase(contentType, "pdf");
//
//        if (!isImage && !isVideo && !isFile) {
//            throw new BusinessException("文件不是图片或视频或正确文件");
//        }

        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();

// 创建图片类型数组
        String img [] = {"jpg", "jpeg", "png", "gif"};
        for (int i = 0; i < img.length; i++) {
            if (img[i].equals(fileType)) {
                type="image";
            }
        }
// 创建文档类型数组
        String document[] = {"doc","xls","pdf","docx" };
        for (int i = 0; i < document.length; i++) {
            if (document[i].equals(fileType)) {
                type="file";
            }
        }
// 创建视频类型数组
        String video[] ={ "mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm" };
        for (int i = 0; i < video.length; i++) {
            if (video[i].equals(fileType)) {
                type="video";
            }
        }

        return type;
    }

    private boolean checkFileType(MultipartFile file) throws IOException {
        boolean isImage = FileTypeUtils.checkFileType(file.getInputStream(),
                EnumFileType.PNG.toString(),
                EnumFileType.JPG.toString(),
                EnumFileType.GIF.toString());

        boolean isVideo = FileTypeUtils.checkFileType(file.getInputStream(),
                EnumFileType.FLV.toString(),
                EnumFileType.MP4.toString(),
                EnumFileType.PNG.toString(),
                EnumFileType.AVI.toString());

        if (!isImage && !isVideo) {
            throw new BusinessException("文件不是图片或视频");
        }
        return isImage;
    }
}
